package com.hycat.mail.product.service.impl;

import com.hycat.mail.common.constant.ProductConstant;
import com.hycat.mail.product.entity.AttrGroupEntity;
import com.hycat.mail.product.service.AttrAttrgroupRelationService;
import com.hycat.mail.product.service.AttrGroupService;
import com.hycat.mail.product.service.CategoryService;
import com.hycat.mail.product.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hycat.mail.common.utils.PageUtil;
import com.hycat.mail.common.utils.Query;

import com.hycat.mail.product.dao.AttrDao;
import com.hycat.mail.product.entity.AttrEntity;
import com.hycat.mail.product.service.AttrService;
import org.springframework.util.StringUtils;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Override
    public PageUtil queryPage(Map<String, Object> params, Integer attrType, Long catelogId) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                getWrapper(params, attrType, catelogId)
        );

        PageUtil pageUtils = new PageUtil(page);
        List<AttrEntity> list = (List<AttrEntity>) pageUtils.getList();
        List<AttrVo> collect = list.stream().map((attrEntity -> {
            AttrVo attrVo = new AttrVo();
            BeanUtils.copyProperties(attrEntity, attrVo);

            String catelogName = categoryService.getById(catelogId).getName();
            attrVo.setCatelogName(catelogName);

            Long attrGroupId = attrAttrgroupRelationService.getByAttrId(attrEntity.getAttrId()).getAttrGroupId();
            String attrGroupName = attrGroupService.getById(attrGroupId).getAttrGroupName();
            attrVo.setAttrGroupName(attrGroupName);
            return attrVo;
        })).collect(Collectors.toList());
        pageUtils.setList(collect);
        return pageUtils;
    }

    @Override
    public void saveDetails(AttrVo attrVo) {
        //save Attr
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrVo, attrEntity);
        save(attrEntity);

        //save AttrGroup
        AttrGroupEntity attrGroupEntity = new AttrGroupEntity();
        BeanUtils.copyProperties(attrVo, attrGroupEntity);
        attrGroupService.save(attrGroupEntity);
    }

    @Override
    public List<AttrEntity> listSearchByIds(List<Long> attrIds) {
        QueryWrapper<AttrEntity> wrapper= new QueryWrapper<AttrEntity>().eq("search_type", 1);
        wrapper.in("attr_id", attrIds);
        return list(wrapper);
    }

    private QueryWrapper<AttrEntity> getWrapper(Map<String, Object> params,Integer attrType, Long catelogId){
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>();

        if(catelogId == 0) {
            wrapper.eq("atty_type", ProductConstant.ATTR_SALE.equals(attrType) ? 0 : 1);
        } else {
            wrapper.eq("catelog_id", catelogId)
                    .eq("atty_type", ProductConstant.ATTR_SALE.equals(attrType) ? 0 : 1);
            String key = (String) params.get("key");
            if(!StringUtils.isEmpty(key)) {
                wrapper.and((obj) -> {
                    obj.like("attr_id", key).or().like("attr_name", key);
                });
            }
        }

        return wrapper;
    }

}